package com.photoeditor.demo.ui.widget.image.edit;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.TextView;

import com.common.base.ui.widget.ripple.RippleLinearLayout;
import com.photoeditor.R;
import com.photoeditor.demo.util.FontUtil;


/**
 * 自定义radio tab按钮(包含图片文字)
 */
public class CustomTabButton extends RippleLinearLayout implements Checkable {


    private boolean mChecked = false;
    private boolean mBroadcasting;
    private OnCheckedChangeListener mOnCheckedChangeListener;
    private OnCheckedChangeListener mOnCheckedChangeWidgetListener;

    private ImageView mImageView;
    private TextView mTextView;

    private int mImageNormalResId;
    private int mImageCheckedResId;
    private int mTextNormalColor = 0;
    private int mTextCheckedColor = 0;

    //选中状态能够点击
    private boolean mCheckedClickable = true;
    //是否可选中（false为不能选中，选中状态是互斥唯一的）
    private boolean mCheckful = true;

    private OnClickListener mOnClickListener;

    public CustomTabButton(Context context) {
        super(context);
        initilize(context);
        LayoutInflater li = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        li.inflate(R.layout.layout_custom_radio_tab_button, this, true);
        mImageView = (ImageView) findViewById(R.id.image);
        mTextView = (TextView) findViewById(R.id.text);
        FontUtil.setCustomFont(mTextView);
    }

    public CustomTabButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        initilize(context);
        initializeWithAttrs(attrs);
    }

//    public CustomTabButton(Context context, AttributeSet attrs, int defStyleAttr) {
//        super(context, attrs, defStyleAttr);
//        initilize(context);
//        initializeWithAttrs(attrs);
//    }

    private void initilize(Context context) {
        setOrientation(VERTICAL);
        setGravity(Gravity.CENTER);
        super.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (isChecked() && !mCheckedClickable) return;
                toggle();
                if (mOnClickListener != null) {
                    mOnClickListener.onClick(v);
                }
            }
        });
    }

    /**
     * 从xml文件解析相关属性
     *
     * @param attrs
     */
    private void initializeWithAttrs(AttributeSet attrs) {
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CustomRaidoTabButton);


        int layout = a.getResourceId(R.styleable.CustomRaidoTabButton_button_layout, R.layout.layout_custom_radio_tab_button);
        LayoutInflater li = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        li.inflate(layout, this, true);
        mImageView = (ImageView) findViewById(R.id.image);
        mTextView = (TextView) findViewById(R.id.text);
        FontUtil.setCustomFont(mTextView);

        mImageNormalResId = a.getResourceId(R.styleable.CustomRaidoTabButton_image, View.NO_ID);
        mImageCheckedResId = a.getResourceId(R.styleable.CustomRaidoTabButton_imageSelected, View.NO_ID);
        if (mImageNormalResId != View.NO_ID) {
            mImageView.setImageResource(mImageNormalResId);
        }


        String text = a.getString(R.styleable.CustomRaidoTabButton_text);
        if (text != null) {
            mTextView.setText(text);
        }

        mTextNormalColor = a.getColor(R.styleable.CustomRaidoTabButton_textColor, 0);
        mTextCheckedColor = a.getColor(R.styleable.CustomRaidoTabButton_textSelectedColor, 0);
        if (!mChecked) {
            if (mTextNormalColor != 0) {
                mTextView.setTextColor(mTextNormalColor);
            }
        } else {
            if (mTextCheckedColor != 0) {
                mTextView.setTextColor(mTextCheckedColor);
            }
        }

        boolean checked = a.getBoolean(R.styleable.CustomRaidoTabButton_checked, false);
        setChecked(checked);

        mCheckedClickable = a.getBoolean(R.styleable.CustomRaidoTabButton_checkedClickable, true);
        mCheckful = a.getBoolean(R.styleable.CustomRaidoTabButton_checkful, true);
        a.recycle();
    }


    private void setImage(int resId) {
        mImageNormalResId = resId;
    }

    private void setCheckedImage(int resId) {
        mImageCheckedResId = resId;
    }

    public void setText(CharSequence text) {
        mTextView.setText(text);
    }

    public void setThemeImageRes(int resId, int checkedResId) {
        mImageNormalResId = resId;
        mImageCheckedResId = checkedResId;
        updateDrawableState();
    }

    /**
     * 设置文字颜色
     *
     * @param normalColor   正常状态颜色
     * @param selectedColor 选中状态颜色
     */
    public void setTextColor(int normalColor, int selectedColor) {
        if (normalColor != 0) {
            mTextNormalColor = normalColor;
            mTextView.setTextColor(normalColor);
        }

        if (selectedColor != 0) {
            mTextCheckedColor = selectedColor;
        }
    }


    /**
     * 更新显示状态
     */
    private void updateDrawableState() {
        if (isChecked()) {
            if (mImageCheckedResId != View.NO_ID) {
                mImageView.setImageResource(mImageCheckedResId);
            }


            if (mTextCheckedColor != 0) {
                mTextView.setTextColor(mTextCheckedColor);
            }
//			setPressed(true);
        } else {
            if (mImageNormalResId != View.NO_ID) {
                mImageView.setImageResource(mImageNormalResId);
            }

            if (mTextNormalColor != 0) {
                mTextView.setTextColor(mTextNormalColor);
            }
//			setPressed(false);
        }

        invalidate();
    }

    /**
     * 设置点击事件
     */
    public void setOnClickListener(OnClickListener clickListener) {
        mOnClickListener = clickListener;
    }

    /**
     * 设置图片和文字的透明度
     *
     * @param alpha
     */
    public void setAlpha(int alpha) {
        mImageView.setAlpha(alpha);
        int textcolor = mTextView.getTextColors().getDefaultColor();
        int red = Color.red(textcolor);
        int green = Color.green(textcolor);
        int blue = Color.blue(textcolor);
        mTextView.setTextColor(Color.argb(alpha, red, green, blue));
    }

    @Override
    public void setChecked(boolean checked) {
        if (mChecked != checked) {
            mChecked = checked;

            updateDrawableState();

            // Avoid infinite recursions if setChecked() is called from a listener
            if (mBroadcasting || !mCheckful) {
                return;
            }

            mBroadcasting = true;
            if (mOnCheckedChangeListener != null) {
                mOnCheckedChangeListener.onCheckedChanged(this, mChecked);
            }
            if (mOnCheckedChangeWidgetListener != null) {
                mOnCheckedChangeWidgetListener.onCheckedChanged(this, mChecked);
            }

            mBroadcasting = false;
        }

    }

    @Override
    public void toggle() {
        setChecked(!mChecked);
    }


    @Override
    public boolean isChecked() {
        return mChecked;
    }

    public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
        mOnCheckedChangeListener = listener;
    }

    void setOnCheckedChangeWidgetListener(OnCheckedChangeListener listener) {
        mOnCheckedChangeWidgetListener = listener;
    }

    /**
     * Interface definition for a callback to be invoked when the checked state
     * of a compound button changed.
     */
    public static interface OnCheckedChangeListener {
        /**
         * Called when the checked state of a compound button has changed.
         *
         * @param buttonView The compound button view whose state has changed.
         * @param isChecked  The new checked state of buttonView.
         */
        void onCheckedChanged(CustomTabButton buttonView, boolean isChecked);
    }


    public TextView getTextView() {
        return mTextView;
    }
}
